/*
 * 版权所有(c)  1952110078@qq.com  2022 - 2022
 */

package com.ldc.module_main.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ldc.commonlibs.model.ProfileModel;
import com.ldc.commonlibs.ui.VMFragmentBase;
import com.ldc.commonlibs.utils.DataSourceUtil;
import com.ldc.commonlibs.utils.ViewModelUtil;
import com.ldc.module_main.adaapter.UserPlayAdapter;
import com.ldc.module_main.databinding.MainFragmentUserPlayBinding;
import com.ldc.module_main.model.UserPlayListModel;
import com.ldc.module_main.viewmodel.UserPlayViewModel;


public class UserPlayFragment extends VMFragmentBase<MainFragmentUserPlayBinding, UserPlayViewModel> {
    private UserPlayAdapter userPlayAdapter;
    private ProfileModel profileModel;

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.getUserPlayListModel().removeObserver(this::userPlayData);
    }

    @Override
    protected void main(@Nullable Bundle savedInstanceState) {
        initView();
        initUserPlayAdapter();
        initData();
    }

    @Override
    protected MainFragmentUserPlayBinding initViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return MainFragmentUserPlayBinding.inflate(inflater, container, false);
    }

    @Override
    protected UserPlayViewModel initViewModel() {
        return ViewModelUtil.create(requireActivity(), UserPlayViewModel.class);
    }

    private void initView() {
        viewBinding.tvUserPlay.setText("---");
    }

    private void initData() {
        profileModel = DataSourceUtil.getProfile();
        viewModel.getUserPlayListModel().observeForever(this::userPlayData);

        viewModel.userPlay(0, "32953014");
        viewBinding.tvUserPlay.setText("我的歌单");

    }

    private void initUserPlayAdapter() {
        userPlayAdapter = new UserPlayAdapter();
        viewBinding.rvUserPlay.setHasFixedSize(true);
        viewBinding.rvUserPlay.setLayoutManager(new LinearLayoutManager(requireActivity(), LinearLayoutManager.HORIZONTAL, false));
        viewBinding.rvUserPlay.setAdapter(userPlayAdapter);
    }

    private void userPlayData(UserPlayListModel data) {
        if (data == null) {
            return;
        }
        userPlayAdapter.addNewData(data.getPlaylist());

    }
}